* 


clear 
edit 
* Paste estimate point from excel
gen n=_n
gen m1=additive
* Model transformations
local i =1
local mod= "additive"
foreach num of numlist 2/4{
replace m`i'= (1.4^`mod')/(10*(1.4^`mod'+1)) if n==`num'
}
replace m`i'=(1-1.4^`mod') if n==5

replace m`i'=(1.4^`mod')/((1.4^`mod')+1)*4 if n==7

replace m`i'=(`mod') if n==8 //level 1 ELIMINATES THE 1.4

foreach num of numlist 9/20{
replace m`i'=m`i'[_n-1]+(`mod')	if n==`num'	//ELIMINATES THE 1.4
}
foreach num of numlist 21/31 {
replace m`i'=(1.4^`mod') if n==`num'		
}
foreach num of numlist 38/45{
replace m`i'=((1.1^`mod')/((1.1^`mod')+1))*4 if n==`num'	 	//by 4
}

saveold "Y:\Fuyao_Erlfang\estimates\pair\cog_add/point_estimates_add_pair.dta", replace

clear 
edit
gen n=_n
*gen m1=additive
gen m2 =multiplicative
local i =2
local mod= "multiplicative"
foreach num of numlist 2/4{
replace m`i'= (1.4^`mod')/(10*(1.4^`mod'+1)) if n==`num'
}
replace m`i'=(1-1.4^`mod') if n==5

replace m`i'=(1.4^`mod')/((1.4^`mod')+1)*4 if n==7

replace m`i'=1+(`mod') if n==8 //1+level 2 ELIMINATES THE 1.4

foreach num of numlist 9/19{
replace m`i'=m`i'[_n-1]+(`mod')	if n==`num'	//ELIMINATES THE 1.4
}
foreach num of numlist 20/30 37{
replace m`i'=(1.4^`mod') if n==`num'		
}
foreach num of numlist 38/45{
replace m`i'=((1.1^`mod')/((1.1^`mod')+1))*4 if n==`num'	 	//by 4
}

saveold "Y:\Fuyao_Erlfang\estimates\pair\cog_mul/point_estimates_mul_pair.dta", replace
 
 * Additive Model
use "Y:\Fuyao_Erlfang\estimates\pair\cog_add/bootsresults.dta", clear 
 
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=(var8) //ELIMINATES THE 1.4
foreach num of numlist 9/20{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')	//ELIMINATES THE 1.4	
}
foreach num of numlist 21/31 {
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 38/45{
gen var`num'_2=((1.1^var`num')/((1.1^var`num')+1))*4		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var31_2 var32-var37 var38_2-var45_2 var46 var47{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}

* TAKING OUT THE OUTLIERS FROM VARIANCE 
foreach var of varlist var25_2 var31_2  {
drop `var'_sd
sort  `var'
gen q=_n
sum `var'
*sum `var' if `var'< r(max)
sum `var' if q<995
gen aux= r(sd)
egen `var'_sd= max(aux) 
drop aux q	
	
}

keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 32/37 46/47{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/31 38/45{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q
rename numero n
merge m:1 n using "Y:\Fuyao_Erlfang\estimates\pair\cog_add/point_estimates_add_pair.dta"
gen out= 1 if m1 > var_ub_ | m1 <var_lb_
br 
pause
drop out 
tostring var_lb_ var_ub_ var_sd_ , replace force format(%6.3f)
gen t = "[" + var_lb_ + "," + var_ub_ + "]" if q==3
replace t = "(" + var_sd_ + ")" if q==2
tostring m1 , replace force format(%6.3f)
replace t=m1  if q ==1

keep q n var1 t
order q n var1 t
replace var1="" if q==2| q==3
saveold "Y:\Fuyao_Erlfang\estimates\pair\cog_add/tempc_add_pair.dta" , replace
gen label=""
replace label= "Point Estimate" if q==1
replace label="Standard Error" if q==2
replace label="Confidence Interval" if q==3
order var1 label t 
drop q n
texsave using "Y:\Fuyao_Erlfang\estimates\pair\cog_add/Cog_add_pair.tex", width(\raggedleft) ///
    replace size(scriptsize) align(ll|c) location(ht) varlabels frag ///
    title(Cognitive Additive) marker(tab:Cog_)  ///
    footnote("Note:", size(scriptsize)) autonumber


* Multiplicative
use "Y:\Fuyao_Erlfang\estimates\pair\cog_mul/bootsresults.dta", clear
* Creating the transformations:
foreach num of numlist 2/4{
gen var`num'_2= (1.4^var`num')/(10*(1.4^var`num'+1))
}
gen var5_2=(1-1.4^var5)

gen var7_2=(1.4^var7)/((1.4^var7)+1)*4

gen var8_2=1+(var8) //ELIMINATES THE 1.4
foreach num of numlist 9/19{
local j = `num'-1
gen var`num'_2=var`j'_2+(var`num')	//ELIMINATES THE 1.4	
}
foreach num of numlist 20/30 37{
gen var`num'_2=(1.4^var`num')		
}
foreach num of numlist 38/45{
gen var`num'_2=((1.1^var`num')/((1.1^var`num')+1))*4		//by 4
}
cap drop q
foreach var of varlist var1 var2_2-var5_2 var6 var7_2-var30_2 var31-var36 var37_2-var45_2 var46 var47{
sort `var'
gen q=_n
gen aux= `var' if q==25
egen `var'_lb= max(aux) 
drop aux
gen aux= `var' if q==975
egen `var'_ub= max(aux) 

drop aux
sum `var'
gen aux= r(sd)
egen `var'_sd= max(aux) 

drop q aux
}

* TAKING OUT THE OUTLIERS FROM VARIANCE 
foreach var of varlist var26_2 var28_2 {
drop `var'_sd
sort  `var'
gen q=_n
sum `var'
*sum `var' if `var'< r(max)
sum `var' if q<997
gen aux= r(sd)
egen `var'_sd= max(aux) 
drop aux q	
	
}
keep var*sd var*lb var*ub 
gen q=_n
foreach num of numlist 1 6 31/36 46/47{
	rename var`num'_lb var_lb_`num'
	rename var`num'_ub var_ub_`num'
	rename var`num'_sd var_sd_`num'
	
}
foreach num of numlist 2/5 7/30 37/45{
	rename var`num'_2_lb var_lb_`num'
	rename var`num'_2_ub var_ub_`num'
	rename var`num'_2_sd var_sd_`num'
	
}
keep if q<4
reshape long var_lb_ var_ub_ var_sd_ , i(q) j(numero)
sort numero q

rename numero n
merge m:1 n using "Y:\Fuyao_Erlfang\estimates\pair\cog_mul/point_estimates_mul_pair.dta"
gen out= 1 if m2 > var_ub_ | m2 <var_lb_
br 
pause
drop out 
tostring var_lb_ var_ub_ var_sd_ , replace force format(%6.3f)
gen t2 = "[" + var_lb_ + "," + var_ub_ + "]" if q==3
replace t2= "(" + var_sd_ + ")" if q==2

tostring  m2, replace force format(%6.3f)
replace t2=m2 if q ==1
keep q n var1 t2
order q n var1 t2
replace var1="" if q==2| q==3
saveold "Y:\Fuyao_Erlfang\estimates\pair\cog_mul/tempc_multi_pair.dta", replace 

gen label=""
replace label= "Point Estimate" if q==1
replace label="Standard Error" if q==2
replace label="Confidence Interval" if q==3
order var1 label t2

drop q n  
edit
*paste manually the levels in a new column
texsave using "Y:\Fuyao_Erlfang\estimates\pair\cog_mul/Cog_Mult_pair.tex", width(\raggedleft) ///
    replace size(scriptsize) align(llcllcc) location(ht) varlabels frag ///
    title(Language Multiplicative) marker(tab:lang_)  ///
    footnote("Note:", size(scriptsize)) autonumber



/* Both

use  "/Volumes/klmChinaReach2/Fuyao_Erlfang/estimates/language/lang_add/templ_add.dta" , clear
merge m:1 n q using "/Volumes/klmChinaReach2/Fuyao_Erlfang/estimates/language/lang_mul/templ_multi.dta"

gen label=""
replace label= "Point Estimate" if q==1
replace label="Standard Error" if q==2
replace label="Confidence Interval" if q==3
order var1 label t t2

drop q n _merge 
texsave using "/Volumes/klmChinaReach2/Fuyao_Erlfang/estimates/language/lang_add/Lang_add_Mult.tex", width(\raggedleft) ///
    replace size(scriptsize) align(ll|cc) location(ht) varlabels frag ///
    title(Language Additive Multiplicative) marker(tab:lang_)  ///
    footnote("Note:", size(scriptsize)) autonumber








